//! Copyright 2012 Xicut Technology, Inc.
//! All rights reserved
//!
#include "Config.h"
#include "StrLib.h"
#include "LedDriver.h"
#include "Sensor.h"
#include "SIM900App.h"
#include "SIM900AppCommand.h"
#include "HostCmdDesc.h"
#include "HostDriver.h"

#include "System.h"

#if defined (__FW_BUILD__)
    #ifdef MACHINE1
        #warning ===================== Firmware nay sai cho may so 1 =====================
    #endif
    #ifdef MACHINE2
        #warning ===================== Firmware nay sai cho may so 2 =====================
    #endif
    #ifdef MACHINE3
        #warning ===================== Firmware nay sai cho may so 3 =====================
    #endif
#endif


cFIFO    cSystem::s_SmsSendFreeFIFO;
U32      cSystem::s_SmsSendFreeFIFO_Storage[cConfig::SIM900_SEND_POOL_SIZE];

void cSystem::InitReset()
{
    s_SmsSendFreeFIFO.Init(s_SmsSendFreeFIFO_Storage, cConfig::SIM900_SEND_POOL_SIZE);
    for (U32 i=0; i<cConfig::SIM900_SEND_POOL_SIZE; ++i)
    {
        s_SmsSendFreeFIFO_Storage[i] = cSIM900AppCommand::Alloc()._;
    }
    cHIDDriver::Init_Reset();
}


bool cSystem::SendSms(U8* phone, U8 phoneLen, U8* msg, U8 msgLen)
{
    if (!((cConfig::MIN_PHONE_COUNT <= phoneLen) && (cConfig::MAX_PHONE_COUNT >= phoneLen) && (cConfig::MAX_SMS_SUPPORT_LENGTH >= msgLen)))
    {
        return (false);
    }

    SIM900AppCommandHandle  smsHandle;
    if (true == s_SmsSendFreeFIFO.Pop(smsHandle._))
    {
        cSIM900AppCommand* pCmd = cSIM900AppCommand::GetPointer(smsHandle);
        pCmd->m_Data.Status = cSmsCmdDesc::SMS_SEND;
        pCmd->m_Data.PhoneLen = phoneLen;
        pCmd->m_Data.MsgLen = msgLen;
        cStrLib::Strcpy(pCmd->m_Data.Phone, phone, phoneLen);
        cStrLib::Strcpy(pCmd->m_Data.Msg, msg, msgLen);
        cSIM900AppCommand::s_SendFIFO.Push(smsHandle._);
        return (true);
    }
    else
    {
        return (false);
    }
}


void cSystem::HostExeCmd(cHostCmdDesc::sHostCmd* pHostCmd)
{
    switch (pHostCmd->Cmd)
    {
    case cHostCmdDesc::POLLING:
        {
            if (true == cStrLib::Strcmp(pHostCmd->mData.Polling.ID, (U8*)MACHINE_ID, MACHINE_ID_LENGTH))
            {
                pHostCmd->Cmd = cHostCmdDesc::POLLING_RPL;
                pHostCmd->mData.Polling.EventID = 0x01;
                pHostCmd->mData.Polling.StatusID = 0x02;
                pHostCmd->mData.Polling.StatusData = 0x0304;
                cHIDDriver::SendMsg((U8*)(pHostCmd), sizeof(cHostCmdDesc::sHostCmd));
            }
        } break;

    case cHostCmdDesc::SEND_SMS:
        {
            SendSms(pHostCmd->mData.Sms.Phone, pHostCmd->PhoneLen, pHostCmd->mData.Sms.Msg, pHostCmd->MsgLen);
            pHostCmd->Cmd = cHostCmdDesc::SEND_SMS_RPL;
            cHIDDriver::SendMsg((U8*)(pHostCmd), sizeof(cHostCmdDesc::sHostCmd));
        } break;

    case cHostCmdDesc::READ_SMS:
        {

        } break;

    default:
        {
            pHostCmd->Cmd = cHostCmdDesc::NULL_CMD;
            cHIDDriver::SendMsg((U8*)(pHostCmd), sizeof(cHostCmdDesc::sHostCmd));
        } break;
    }
}


void cSystem::RunOne()
{
    U8 len;
    cHostCmdDesc::sHostCmd* pCmd = (cHostCmdDesc::sHostCmd*)(cHIDDriver::GetIncomeMsg(len));
    if (0 != pCmd)
    {
        HostExeCmd(pCmd);
    }

    SIM900AppCommandHandle handle;
    if (true == cSIM900AppCommand::s_SendStatusFIFO.Pop(handle._))
    {
        s_SmsSendFreeFIFO.Push(handle._);
    }
}
